package com.yupi.springbootinit.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.yupi.springbootinit.utils.RabbitMqUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class TtlProducer {

    private final static String QUEUE_NAME = "ttl_queue";

    private static final String EXCHANGE_NAME = "ttl-exchange";

    private static final String ROUTING_KEY = "ttl-routing-key";

    public static void main(String[] argv) throws Exception {

        // 建立连接、创建频道
        try (Channel channel = RabbitMqUtils.getChannel()) {
            Map<String, Object> headers = new HashMap<>();
            headers.put("lxr", "0122");
            channel.exchangeDeclare(EXCHANGE_NAME, "headers", false, false, headers);
            // 发送消息
            String message = "Hello World!";

            // 给消息指定过期时间
            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                    .expiration("10000")
                    .headers(headers)
                    .build();
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, properties, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}